package it.getta.vicinometro.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public abstract class SwipeIndicatorAdapter extends BaseAdapter implements SpinnerAdapter {

	protected Context mContext;
	protected LinearLayout mSwipeContainer;
	private RadioGroup mRadioGroup;
	protected int mSelectorResId = -1;
	protected LayoutInflater mInflater;
	private TextView mCounterTextView;
	protected int mSelectedItemIndex = -1;

	public SwipeIndicatorAdapter(Context context) {
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public SwipeIndicatorAdapter(Context context, int selectorResId) {
		mContext = context;
		mSelectorResId = selectorResId;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setSwipeIndicatorContainer(LinearLayout swipeIndicatorContainer) {
		mSwipeContainer = swipeIndicatorContainer;
	}
	
	public void setCounterTextView(TextView textView) {
		mCounterTextView = textView;
	}

	private void setUpIndicator() {

		if (mSwipeContainer != null) {
			int posChecked = -1;
			if (mRadioGroup != null) {
				posChecked = mRadioGroup.getCheckedRadioButtonId();
			}
			mSwipeContainer.removeAllViews();
			mRadioGroup = new RadioGroup(mContext);
			mRadioGroup.setOrientation(LinearLayout.HORIZONTAL);
			for (int i = 0; i < getCount(); i++) {
				RadioButton radio = new RadioButton(mContext);
				radio.setId(i);
				if (mSelectorResId != -1) {
					radio.setButtonDrawable(mSelectorResId);
				}
				radio.setClickable(false);
				LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(16, 16);
				layoutParams.setMargins(4, 0, 4, 0);
				mRadioGroup.addView(radio, layoutParams);
			}
			mSwipeContainer.addView(mRadioGroup);
			if (posChecked != -1) {
				setSelectedIndicator(posChecked);
			}

		} else if(mCounterTextView != null) {
			setCounterText();
		}
	}

	public void setSelectedIndicator(int pos) {
		if (mSwipeContainer != null) {
			mRadioGroup.check(pos);
		}
		mSelectedItemIndex = pos;
		setCounterText();
	}
	
	private void setCounterText() {
		if (mCounterTextView != null) {
			int total = getCount();
			int selected = mSelectedItemIndex + 1;
			if (selected != -1) {
				mCounterTextView.setText(selected + "/" + total);
			}
		}
	}

	public void clearAllSelections() {
		mRadioGroup.clearCheck();
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		setUpIndicator();
	}

	@Override
	public void notifyDataSetInvalidated() {
		super.notifyDataSetInvalidated();
	}

	@Override
	public abstract int getCount();

	@Override
	public abstract Object getItem(int position);

	@Override
	public abstract long getItemId(int position);

	@Override
	public abstract View getView(int position, View convertView, ViewGroup parent);
	
}
